Modules and Packages: An Overview

In Python, a module is a file containing Python definitions and statements. These can be functions, classes, variables or any combination thereof. Modules are used to organize code and make it reusable. They can be imported into other Python scripts and used as a dependency.

Packages, on the other hand, are a way of organizing related modules into a single hierarchy. A package is a directory containing modules and subpackages. A subpackage is a package within a package. Packages provide a way of creating a hierarchical organization of modules that can be used to organize large Python projects.

Importing Modules and Packages

To use a module, it must first be imported into the current script. This can be done using the import statement followed by the module name:

import module_name

Alternatively, specific functions or variables within a module can be imported using the from keyword:

from module_name import function_name

To import all functions and variables within a module, use the * wildcard:

from module_name import *

To import a subpackage, use the dot notation:

import package_name.subpackage_name

Creating Modules

Creating a module is as simple as creating a new Python file and defining some functions or variables. For example, the following code defines a module called "my_module" with a single function called "hello_world":

def hello_world(): print("Hello, world!")

The module can then be used in other scripts by importing it using the import statement:

import my_module my_module.hello_world() # prints "Hello, world!"

Creating Packages

Creating a package is similar to creating a module, but instead of a single file, a package is a directory containing one or more Python files. The directory must also contain a special file called init.py, which is executed when the package is imported.

For example, suppose we have a directory called "my_package" containing two Python files:

my_package/ __init__.py module1.py

The init.py file can define package-level variables and functions, and import modules and subpackages within the package:

# __init__.py from .module1 import foo from .subpackage import bar PACKAGE_LEVEL_VAR = 42 def baz(): print("baz") __all__ = ["foo", "bar", "baz", "PACKAGE_LEVEL_VAR"]

The module1.py file can define functions and variables specific to that module:

# module1.py def foo(): print("foo")

Subpackages can be created by adding more directories within the package directory:

my_package/ __init__.py module1.py subpackage/ __init__.py module2.py

The init.py file in the subpackage directory can import modules within the subpackage:

# subpackage/__init__.py from .module2 import qux SUBPACKAGE_LEVEL_VAR = 99 __all__ = ["qux", "SUBPACKAGE_LEVEL_VAR"]

The module2.py file can define functions and variables specific to that subpackage:

# subpackage/module2.py def qux(): print("qux")

The package and subpackages can then be imported into other Python scripts:

import my_package my_package.foo() # prints "foo" my_package.subpackage.qux() # prints "qux" print(my_package.PACKAGE_LEVEL_VAR) # prints "42" print(my_package.subpackage.SUBPACKAGE_LEVEL_VAR) # prints "99"

Conclusion

Modules and packages are essential for organizing code and making it reusable. Modules allow for individual pieces of code to be imported and used in other scripts, while packages provide a way to organize related modules into a hierarchy. By understanding how to create and use modules and packages, you can write more maintainable and reusable Python code.

モジュールとパッケージ[JA]